package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * @Title: ProductServiceImpl
 * @Package: com.project.service.impl
 * @Description:
 * @Author: Liang-Xie
 */
@SuppressWarnings("all")
@Service
@Transactional
public class ProductServiceImpl implements IProductService {

    @Autowired
    private IProductMapper productMapper;

    @Override
    public IPage<ProductBean> findByItem(Integer pageNO, String name, Integer startNum, Integer endNum, String info, String type) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        if (name != null && name.length() != 0) {
            qw.like("p_name", name);
        }
        if (startNum != null) {
            qw.ge("p_num", startNum);
        }
        if (endNum != null) {
            qw.le("p_num", endNum);
        }
        if (info != null && info.length() != 0) {
            if (info.equals("未定义销售价")) {
                qw.isNull("p_sellPrice");
            }
            if (info.equals("定义销售价")) {
                qw.isNotNull("p_sellPrice");
            }
        }
        if (type != null && type.length() != 0) {
            qw.eq("p_type", type);
        }


        return productMapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public ProductBean findById(String productId) {
        return productMapper.selectById(productId);
    }

    @Override
    public void updatePrice(String productId, Integer price) {
        ProductBean product = productMapper.selectById(productId);
        product.setSellPrice(price);
        productMapper.updateById(product);
    }
}
